#### R codes for
#### Spatial Polarization, Partisan Climate, and Participatory Actions: 
#### Do Congenial Contexts Lead to Mobilization, Resignation, Activation, or Complacency?
#### Accepted for publication in Political Behavior (2022)
#### By Jiyoun Suk, Ph.D., Douglas McLeod, Ph.D., & Dhavan Shah, Ph.D.

library(lme4)
library(robustlmm)
library(sjPlot)

final <- read.csv("pol_behav_final_datasest.csv")

## Variable construction
final$partyID_lean <- NA
final$partyID_lean[final$partyID ==1] = 1
final$partyID_lean[final$partyID ==2] =1
final$partyID_lean[final$partyID ==3] =1
final$partyID_lean[final$partyID ==4] =2
final$partyID_lean[final$partyID ==5] =3
final$partyID_lean[final$partyID ==6] =3
final$partyID_lean[final$partyID ==7] =3

final$partyID_lean <- factor(final$partyID_lean, levels=c(1:3),
                           labels = c("Dem w leaners", "Pure Ind", "Rep w leaners"))

final$ag.media <- with(final, ifelse(partyID_cat=="Dem", (npr + msnbc + libblog)/2, 
                                     ifelse(partyID_cat=="Rep", (fox + conradio + conblog)/2, NA)))
final$disag.media <- with(final, ifelse(partyID_cat=="Rep", (npr + msnbc + libblog)/2, 
                                        ifelse(partyID_cat=="Dem", (fox + conradio + conblog)/2, NA)))

final$net.ag.media <- final$ag.media - final$disag.media
final$net.ag.talk <- final$ag.talk3 - final$disag.talk3

final$age_r <- scale(final$age)[,1]
final$edu_r <- scale(final$edulevel)[,1]
final$income_r <- scale(final$income)[,1]
final$underrep_r <- scale(final$underrep)[,1]
final$strength_r <- scale(final$partisanship)[,1]
final$polint_r <- scale(final$polint)[,1]
final$net.ag.media_r <- scale(final$net.ag.media)[,1]
final$net.ag.talk_r <- scale(final$net.ag.talk)[,1]

final$female <- as.factor(final$gender)
final$partisan_f <- as.factor(final$partisanship)
final$STATE <- as.factor(final$STATE)
final$zip <- as.factor(final$zip)

final$resid.off <- resid(lm(offline~online, data=final, na.action=na.exclude))
final$resid.on <- resid(lm(online~offline, data=final, na.action=na.exclude))

## Main analysis
### All sample
offpart <- rlmerRcpp(resid.off ~ age_r + female + edu_r + income_r + white +  strength_r + polint_r +net.ag.media_r + net.ag.talk_r +  unfav_vot_zip2 + underrep + day1+day2+day3+day4+day5+day6+day7+day8+day9+day10+day11+day12+day13+day14+day15+day16+day17+day18+day19+day20+day21+day22+day23+day24+day25+day26+day27+day28+day29+day30+day31+day32+day33+day34+day35+day36+day37+day38+day39+day40+day41+day42+day43+day44+day45+day46+day47+day48+day49+day50+day51+day52+day53+day54+day5 +(1|zip) + (1|STATE), data=final)
#Or you could included factor(data_cat) instead of a list of daily dummy variables, which produces the same results.
summary(offpart)
tab_model(offpart, digits=3, show.se = TRUE)

onpart <- rlmerRcpp(resid.on ~ age_r + female + edu_r + income_r + white +  strength_r + polint_r +  net.ag.media_r + net.ag.talk_r +unfav_vot_zip2 + underrep + day1+day2+day3+day4+day5+day6+day7+day8+day9+day10+day11+day12+day13+day14+day15+day16+day17+day18+day19+day20+day21+day22+day23+day24+day25+day26+day27+day28+day29+day30+day31+day32+day33+day34+day35+day36+day37+day38+day39+day40+day41+day42+day43+day44+day45+day46+day47+day48+day49+day50+day51+day52+day53+day54+day5 +(1|zip) + (1|STATE), data=final)
summary(onpart)
tab_model(onpart, digits=3, show.se = TRUE)

### Partisan subgroup
rep = subset(final, partyID_lean=="Rep w leaners") #Republican data subset
dem = subset(final, partyID_lean=="Dem w leaners") #Democrat data subset
#### Republican subset
offpart.rep <- rlmerRcpp(resid.off ~ age_r + female + edu_r + income_r + white + strength_r + polint_r +  net.ag.media_r +net.ag.talk_r + unfav_vot_zip2 + underrep +  day1+day2+day3+day4+day5+day6+day7+day8+day9+day10+day11+day12+day13+day14+day15+day16+day17+day18+day19+day20+day21+day22+day23+day24+day25+day26+day27+day28+day29+day30+day31+day32+day33+day34+day35+day36+day37+day38+day39+day40+day41+day42+day43+day44+day45+day46+day47+day48+day49+day50+day51+day52+day53+day54+day55+(1|zip) + (1|STATE), data=rep)
summary(offpart.rep)
tab_model(offpart.rep, digits=3, show.se = TRUE)

onpart.rep <- rlmerRcpp(resid.on ~ age_r + female + edu_r + income_r + white + strength_r + polint_r + net.ag.media_r + net.ag.talk_r + unfav_vot_zip2 + underrep +  day1+day2+day3+day4+day5+day6+day7+day8+day9+day10+day11+day12+day13+day14+day15+day16+day17+day18+day19+day20+day21+day22+day23+day24+day25+day26+day27+day28+day29+day30+day31+day32+day33+day34+day35+day36+day37+day38+day39+day40+day41+day42+day43+day44+day45+day46+day47+day48+day49+day50+day51+day52+day53+day54+day5 + (1|zip) + (1|STATE), data=rep)
summary(onpart.rep)
tab_model(onpart.rep, digits=3, show.se = TRUE)

#### Democrat subset
offpart.dem <- rlmerRcpp(resid.off ~ age_r + female + edu_r + income_r + white + strength_r + polint_r +  net.ag.media_r +net.ag.talk_r + unfav_vot_zip2 + underrep +  day1+day2+day3+day4+day5+day6+day7+day8+day9+day10+day11+day12+day13+day14+day15+day16+day17+day18+day19+day20+day21+day22+day23+day24+day25+day26+day27+day28+day29+day30+day31+day32+day33+day34+day35+day36+day37+day38+day39+day40+day41+day42+day43+day44+day45+day46+day47+day48+day49+day50+day51+day52+day53+day54+day5+(1|zip) + (1|STATE), data=dem)
summary(offpart.dem)
tab_model(offpart.dem, digits=3, show.se = TRUE)

onpart.dem <- rlmerRcpp(resid.on ~ age_r + female + edu_r + income_r + white + strength_r + polint_r +  net.ag.media_r + net.ag.talk_r +unfav_vot_zip2 + underrep +  day1+day2+day3+day4+day5+day6+day7+day8+day9+day10+day11+day12+day13+day14+day15+day16+day17+day18+day19+day20+day21+day22+day23+day24+day25+day26+day27+day28+day29+day30+day31+day32+day33+day34+day35+day36+day37+day38+day39+day40+day41+day42+day43+day44+day45+day46+day47+day48+day49+day50+day51+day52+day53+day54+day5+(1|zip) + (1|STATE), data=dem)
summary(onpart.dem)
tab_model(onpart.dem, digits=3, show.se = TRUE)

### Political interest
highpolint = subset(final, polint >3|polint==3)
lowpolint = subset(final, polint <3)
#### High political interest group
offpart.polhigh <- rlmerRcpp(resid.off ~ age_r + female + edu_r + income_r + white +  strength_r + net.ag.talk_r +  net.ag.media_r + unfav_vot_zip2 + +underrep + day1+day2+day3+day4+day5+day6+day7+day8+day9+day10+day11+day12+day13+day14+day15+day16+day17+day18+day19+day20+day21+day22+day23+day24+day25+day26+day27+day28+day29+day30+day31+day32+day33+day34+day35+day36+day37+day38+day39+day40+day41+day42+day43+day44+day45+day46+day47+day48+day49+day50+day51+day52+day53+day54+day55+(1|zip) + (1|STATE), data=highpolint)
summary(offpart.polhigh)

onpart.polhigh <- rlmerRcpp(resid.on ~ age_r + female + edu_r + income_r + white +  strength_r + net.ag.talk_r +  net.ag.media_r + unfav_vot_zip2 + +underrep + day1+day2+day3+day4+day5+day6+day7+day8+day9+day10+day11+day12+day13+day14+day15+day16+day17+day18+day19+day20+day21+day22+day23+day24+day25+day26+day27+day28+day29+day30+day31+day32+day33+day34+day35+day36+day37+day38+day39+day40+day41+day42+day43+day44+day45+day46+day47+day48+day49+day50+day51+day52+day53+day54+day55 +(1|zip) + (1|STATE), data=highpolint)
summary(offpart.polhigh)

#### Low political interest group
offpart.pollow <- rlmerRcpp(resid.off ~ age_r + female + edu_r + income_r + white +  strength_r + net.ag.talk_r +  net.ag.media_r + unfav_vot_zip2 + +underrep + day1+day2+day3+day4+day5+day6+day7+day8+day9+day10+day11+day12+day13+day14+day15+day16+day17+day18+day19+day20+day21+day22+day23+day24+day25+day26+day27+day28+day29+day30+day31+day32+day33+day34+day35+day36+day37+day38+day39+day40+day41+day42+day43+day44+day45+day46+day47+day48+day49+day50+day51+day52+day53+day54+day55+(1|zip) + (1|STATE), data=lowpolint)
summary(offpart.pollow)

onpart.pollow <- rlmerRcpp(resid.on ~ age_r + female + edu_r + income_r + white +  strength_r + net.ag.talk_r +  net.ag.media_r + unfav_vot_zip2 + +underrep + day1+day2+day3+day4+day5+day6+day7+day8+day9+day10+day11+day12+day13+day14+day15+day16+day17+day18+day19+day20+day21+day22+day23+day24+day25+day26+day27+day28+day29+day30+day31+day32+day33+day34+day35+day36+day37+day38+day39+day40+day41+day42+day43+day44+day45+day46+day47+day48+day49+day50+day51+day52+day53+day54+day55+(1|zip) + (1|STATE), data=lowpolint)
summary(onpart.pollow)

